package com.molyfun.parents.view;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;

import com.molyfun.parents.activity.common.GameViewerActivity;
import com.molyfun.parents.activity.common.ImageViewerActivity;
import com.molyfun.parents.activity.setting.adapter.FileTabAdapter;
import com.molyfun.parents.impl.PlanDetailTabCallback;
import com.molyfun.parents.rest.RestClient;
import com.molyfun.parents.rest.bean.PlanDetailResponse;

import java.util.List;

/**
 * Created by william(liuchao.hit@gmail.com).
 * 底部横条的TabBar
 */
public class FileListView extends RecyclerView implements PlanDetailTabCallback {

    protected LayoutInflater layoutInflater;

    private int mChooseTab = 0;

    private Context context;

    LinearLayoutManager mLinearLayoutManager;
    private FileTabAdapter mFileTabAdapter;

    private List<PlanDetailResponse.PlanDetailBean.AnalysislistBean> mTabsList;

    public FileListView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public FileListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public FileListView(Context context) {
        super(context);
        init(context, null);
    }

    public void initDataList(List<PlanDetailResponse.PlanDetailBean.AnalysislistBean> list){
        this.mTabsList = list;
        mFileTabAdapter.refreshData(list);
        mFileTabAdapter.notifyDataSetChanged();
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        mLinearLayoutManager = new LinearLayoutManager(context);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        setLayoutManager(mLinearLayoutManager);
//        layoutInflater = LayoutInflater.from(context);
//        layoutInflater.inflate(R.layout.widget_attachment, this);
        setHasFixedSize(true);

        mFileTabAdapter = new FileTabAdapter<PlanDetailResponse.PlanDetailBean.AnalysislistBean>(context, this);
        setAdapter(mFileTabAdapter);
    }

    @Override
    public void onPlanDetailTabClick(int position) {
        System.out.println("onPlanDetailTabClick---->" + position);
        PlanDetailResponse.PlanDetailBean.AnalysislistBean analysislistBean = mTabsList.get(position);
        System.out.println("analysislistBean---->" + analysislistBean.getName());
        System.out.println("analysislistBean---->" + analysislistBean.getUrl());
        if ((analysislistBean.getName().toLowerCase()).trim().contains("jpg") || analysislistBean.getName().toLowerCase().trim().contains("png")) {
            ImageViewerActivity.startActivity(context, analysislistBean.getUrl());
        } else {
            GameViewerActivity.startActivity(context, RestClient.ONLINE_OFFICE_FILE_URL_HEAD + analysislistBean.getUrl());
        }
    }

    public int getCurrentChooseTab(){
        return mChooseTab;
    }

}